﻿using SuperX.Common.Log;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;

namespace SuperX.DataCenterService.WebAPI
{
    internal class WebApiHost
    {
        private DataCenterConfig Config;
        private IWebHost server;

        public WebApiHost(DataCenterConfig config)
        {
            this.Config = config;
            server = WebHost.CreateDefaultBuilder()
                .UseUrls($"http://*:{Config.WebAPIConfig.PORT}")
                .UseStartup<WebHostCore>()
                .Build();
        }

        public bool Start()
        {
            try
            {
                server.StartAsync();
                Logger.Info($"WebApi服务正在运行 ... @ http://*:{Config.WebAPIConfig.PORT}");
            }
            catch (Exception ex)
            {
                Logger.Error("Error:" + ex.Message);
            }
            return true;
        }

        public bool Stop()
        {
            try
            {
                server?.StopAsync();
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }
            return true;
        }
    }
}